package com.cjh.wxdemoserver.base.result;

import com.cjh.wxdemoserver.base.enums.CommonResultCode;
import com.cjh.wxdemoserver.base.exception.BizException;
import lombok.Getter;
import lombok.Setter;

import java.text.MessageFormat;

/**
 * @author ChenJH
 * @description 请求相应数据
 */

@Getter
@Setter
public class Result<T> {
    private final int code;
    private final String msg;
    private final T data;
    
    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> ok() {
        return ok(CommonResultCode.SUCCESS, null);
    }
    public static <T> Result<T> ok(T data) {
        return ok(CommonResultCode.SUCCESS, data);
    }
    public static <T> Result<T> ok(CommonResultCode resultCode, T data) {
        return new Result<>(resultCode.getCode(), resultCode.getMsg(), data);
    }
    public static <T> Result<T> error(int code, String msg) {
        return new Result<>(code, msg, null);
    }
    public static <T> Result<T> error(int code, String msg, T data) {
        return new Result<>(code, msg, data);
    }
    public static <T> Result<T> error(int code, String msg, Object... args) {
        return new Result<>(code, MessageFormat.format(msg, args), null);
    }
    public static <T> Result<T> error(int code, String msg, T data, Object... args) {
        return new Result<>(code, MessageFormat.format(msg, args), data);
    }
    public static <T> Result<T> error(CommonResultCode resultCode) {
        return new Result<>(resultCode.getCode(), resultCode.getMsg(), null);
    }
    public static <T> Result<T> error(CommonResultCode resultCode, Object... args) {
        return new Result<>(resultCode.getCode(), MessageFormat.format(resultCode.getMsg(), args), null);
    }
    public static <T> Result<T> error(CommonResultCode resultCode, T data) {
        return new Result<>(resultCode.getCode(), resultCode.getMsg(), data);
    }
    public static <T> Result<T> error(CommonResultCode resultCode, T data, Object... args) {
        return new Result<>(resultCode.getCode(), MessageFormat.format(resultCode.getMsg(), args), data);
    }

    public static Result<Void> error(BizException e) {
        return new Result<>(e.getCode(), e.getMsg(), null);
    }
}
